Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import { eq } from 'drizzle-orm' import { NextResponse } from 'next/server' import { db, schema } from '@/db' import { getLinkedParentIds, getTeacherClassroom, isParentOf, unenrollStudent, } from '@/lib/classroom' import { emitStudentUnenrolled } from '@/lib/classroom/socket-emitter' import { getUserId } from '@/lib/viewer' import { withAuth } from '@/lib/auth/withAuth' /** * DELETE /api/classrooms/[classroomId]/enrollments/[playerId] * Unenroll student from classroom (teacher or parent) * * Returns: { success: true } */ export const DELETE = withAuth(async (_request, { params }) => { try { const { classroomId, playerId } = (await params) as { classroomId: string; playerId: string } const userId = await getUserId() // Check authorization: must be teacher of classroom OR parent of student const classroom = await getTeacherClassroom(userId) const isTeacher = classroom?.id === classroomId const parentCheck = await isParentOf(userId, playerId) if (!isTeacher && !parentCheck) { return NextResponse.json( { error: 'Must be the classroom teacher or a parent of the student' }, { status: 403 } ) } await unenrollStudent(classroomId, playerId) // Emit socket event for real-time updates try { // Get classroom and player info for socket event const [classroomInfo] = await db .select({ name: schema.classrooms.name }) .from(schema.classrooms) .where(eq(schema.classrooms.id, classroomId)) .limit(1) const [playerInfo] = await db .select({ name: schema.players.name }) .from(schema.players) .where(eq(schema.players.id, playerId)) .limit(1) if (classroomInfo && playerInfo) { // Get parent IDs to notify const parentIds = await getLinkedParentIds(playerId) await emitStudentUnenrolled( { classroomId, classroomName: classroomInfo.name, playerId, playerName: playerInfo.name, unenrolledBy: isTeacher ? 'teacher' : 'parent', }, { classroomId, // Teacher sees student removed userIds: parentIds, // Parents see child is no longer enrolled playerIds: [playerId], // Student sees they're no longer in classroom } ) } } catch (socketError) { console.error('[Unenroll] Failed to emit socket event:', socketError) } return NextResponse.json({ success: true }) } catch (error) { console.error('Failed to unenroll student:', error) return NextResponse.json({ error: 'Failed to unenroll student' }, { status: 500 }) } }) |